package com.yimi.yinhepda.adapter.RecycleAdapter

import android.content.Context
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.pda.ym.base.CommonRecycleAdapter
import com.pda.ym.base.CommonViewHolder
import com.pda.ym.base.MultiTypeSupport
import com.pda.ym.db.table.CarPlateTable
import com.pda.ym.entity.bean.CarPlanTaskEntity
import com.yimi.yinhepda.R
import com.yimi.yinhepda.db.dao.CarPlateTableDao
import com.yimi.yinhepda.utils.TransInformation
import com.yimi.yinhepda.views.load_module.LoadUtil
import kotlinx.android.synthetic.main.activity_new_load_task.*
import kotlinx.android.synthetic.main.select_car_plate.*

/**
 * @author: lj
 * Date: 2020/3/9 14:28
 * Description:装车任务适配器
 */
class NewLoadTaskAdapter(var context: Context?, dataList: MutableList<CarPlanTaskEntity>?,
                         var commonClickListener: CommonViewHolder.onItemCommonClickListener?,
                         multiTypeSupport: MultiTypeSupport<CarPlanTaskEntity>,
                         var onclick: View.OnClickListener)
    : CommonRecycleAdapter<CarPlanTaskEntity>(context, dataList, multiTypeSupport) {

    override fun bindData(holder: CommonViewHolder?, data: CarPlanTaskEntity?, position: Int) {

        if (position == 0) {
            //第一项为搜索条件
            holder?.getView<Button>(R.id.btn_selectNextDept)?.setOnClickListener(onclick)
            val mBtnCarPlate = holder?.getView<Button>(R.id.btn_carPlate)
            mBtnCarPlate?.setOnClickListener(onclick)
            holder?.getView<Button>(R.id.btn_down)?.setOnClickListener(onclick)

            val carNo = holder?.getView<EditText>(R.id.et_carNo)
            //转换成大写
            carNo?.transformationMethod = TransInformation()
            if(carNo?.text.toString() == ""){
                //初始设置车牌
                val carPlateTables: List<CarPlateTable> = CarPlateTableDao().getAllData()
                if (!carPlateTables.isNullOrEmpty()) {
                    mBtnCarPlate?.text = carPlateTables[0].carPlate
                }
            }

        } else {

            holder?.setCommonClickListener(commonClickListener)

            val tvCarNo = holder?.getView<TextView>(R.id.tv_carNo)
            tvCarNo?.text = data?.vehNo

            holder?.setText(R.id.tv_taskNo, data?.taskNo)
            holder?.setText(R.id.tv_route_line, data?.vehLineName)
            holder?.setText(R.id.tv_plan_start_time, data?.planStartTime)
            if (data?.shift.isNullOrEmpty()) {
                holder?.setText(R.id.tv_shift, "")
            } else {
                holder?.setText(R.id.tv_shift, String.format("%s班次", LoadUtil.getShift(data?.shift)))
            }
            holder?.getView<LinearLayout>(R.id.ll_setOut_sealCar_item)?.isSelected = data?.isCheck!!
            tvCarNo?.isSelected = data.isCheck

            var hasStatus = false
            data.loadTasks?.forEach { loadTask ->
                if (loadTask.status == 2) {
                    hasStatus = true
                }
            }

            if (hasStatus) {
                hasStatus = true
                holder?.getView<LinearLayout>(R.id.ll_setOut_sealCar_item)?.isActivated = true
                tvCarNo?.isActivated = true
            } else {
                holder?.getView<LinearLayout>(R.id.ll_setOut_sealCar_item)?.isActivated = false
                tvCarNo?.isActivated = false
            }

        }

    }
}